<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/css/main.css" type="text/css">
    <title>ECharts</title>
</head>
<body>
<div id="container">
    <div id="main"></div>
</div>
</body>
</html>
<script src="/js/echarts.min.js"></script>
<script src="/js/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
    let myChart = echarts.init(document.getElementById('main'));
    let option = {
        backgroundColor: '#061326',
        title: {
            text: "TITLE",
            left: 'center',
            top: '3%',
            textStyle: {
                fontSize: 20,
                color: '#FFF'
            }
        },
        grid: {
            top: "5%",
            left: "0%",
            bottom: "5%",
            right: "0%",
            containLabel: true
        },
        tooltip: {
            show: true
        },
        animation: false,
        xAxis: [{
            type: "category",
            data: [],
            axisTick: {
                alignWithLabel: true
            },
            nameTextStyle: {
                color: "#82b0ec"
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: "#82b0ec"
                }
            },
            axisLabel: {
                textStyle: {
                    color: "#fff",
                    fontSize: 16
                },
                margin: 30
            }
        }],
        yAxis: [{
            show: false,
            type: "value",
            axisLabel: {
                textStyle: {
                    color: "#fff"
                },
            },
            splitLine: {
                lineStyle: {
                    color: "#0c2c5a"
                }
            },
            axisLine: {
                "show": false
            }
        }],
        series: [{
            name: "",
            type: 'pictorialBar',
            symbolSize: [40, 10],
            symbolOffset: [0, -6],
            symbolPosition: 'end',
            z: 12,
            label: {
                normal: {
                    show: true,
                    position: "top",
                    fontSize: 16,
                    fontWeight: 'bold',
                    color: '#34DCFF'
                }
            },
            color: "#2DB1EF",
            data: []
        }, {
                name: '',
                type: 'pictorialBar',
                symbolSize: [40, 10],
                symbolOffset: [0, 7],
                z: 12,
                color: "#2DB1EF",
                data: []
            },
            {
                name: '',
                type: 'pictorialBar',
                symbolSize: [50, 15],
                symbolOffset: [0, 12],
                z: 10,
                itemStyle: {
                    normal: {
                        color: 'transparent',
                        borderColor: '#2EA9E5',
                        borderType: 'solid',
                        borderWidth: 1
                    }
                },
                data: []
            },
            {
                name: '',
                type: 'pictorialBar',
                symbolSize: [70, 20],
                symbolOffset: [0, 18],
                z: 10,
                itemStyle: {
                    normal: {
                        color: 'transparent',
                        borderColor: '#19465D',
                        borderType: 'solid',
                        borderWidth: 2
                    }
                },
                data: []
            },
            {
                type: 'bar',
                "barWidth": "40",
                barGap: '10%',
                barCateGoryGap: '5%',
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [{
                            offset: 0,
                            color: "#38B2E6"
                        },
                            {
                                offset: 1,
                                color: "#0B3147"
                            }
                        ]),
                        opacity: .8
                    },
                },
                data: []
            }
        ]
    };
    myChart.setOption(option);
    myChart.showLoading();
    $.ajax({
        type : "get",
        async : true,
        url : '/bar/one/1',
        data : {},
        dataType : "json",
        success : function(result) {
            if (result.code == 200) {
                myChart.hideLoading();
                if(Array.isArray(option.xAxis)) {
                    result.data.xAxis.forEach(function (item, index) {
                        option.xAxis[index].data = item.data;
                        option.xAxis[index].type = item.type;
                        if (item.name) {
                            option.xAxis[index].name = item.name;
                        }
                    });
                }else {
                    let item = result.data.xAxis[0];
                    option.xAxis.data = item.data;
                    option.xAxis.type = item.type;
                    if (item.name) {
                        option.xAxis.name = item.name;
                    }
                }

                if(Array.isArray(option.yAxis)) {
                    result.data.yAxis.forEach(function (item, index) {
                        option.yAxis[index].type = item.type;
                        option.yAxis[index].name = item.name;
                        if (item.data) {
                            option.yAxis[index].data = item.data;
                        }
                    });
                }else {
                    let item = result.data.yAxis[0];
                    option.yAxis.type = item.type;
                    option.yAxis.name = item.name;
                    if (item.data) {
                        option.yAxis.data = item.data;
                    }
                }

                option.series.forEach(function (item,index) {
                    item.data = result.data.series[0].data;
                });

                if (result.data.legend && option.legend) {
                    option.legend.data = result.data.legend;
                }
                if (result.data.title) {
                    option.title.text = result.data.title;
                }
                myChart.setOption(option);
            }
        },
        error : function(errorMsg) {
            alert("图表请求数据失败!");
            myChart.hideLoading();
        }
    });
</script>
